home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 56194 / 56194.xpi / chrome / topsite.jar / content / js / toolbarView.js < prev    next >
Text File  |  2010-01-17  |  8KB  |  220 lines

  1. if (!topsite) var topsite={};
  2.  
  3. topsite.toolbarView = function(topicsContainer, getSitesFunc, onSiteClickFunc) {
  4.     this.topicsContainer = topicsContainer;
  5.     this.chevron = topicsContainer.parentNode.lastChild;
  6.     this.getSitesFunc = getSitesFunc;
  7.     this.onSiteClickFunc = onSiteClickFunc;
  8.     
  9.     this.populate = function(arrTags){        
  10.         for (var i = 0; i < arrTags.length; ++i){
  11.             var strTag = arrTags[i];
  12.             strTag = topsite.common.capitalizeWords(strTag);
  13.             var newToolbarButton = document.createElement("toolbarbutton");
  14.             newToolbarButton.setAttribute("label", strTag);
  15.             newToolbarButton.setAttribute("class", "relatedTopicButton");
  16.             newToolbarButton.setAttribute("type", "menu");
  17.             newToolbarButton.setAttribute("image", "chrome://topsite/content/images/category" + i + ".png");
  18.             this.topicsContainer.appendChild(newToolbarButton);
  19.             var newMenuInButton = document.createElement("menupopup");
  20.             newToolbarButton.appendChild(newMenuInButton);
  21.             var myView = this;            
  22.             newToolbarButton.addEventListener("popupshowing", function f(event){
  23.                 myView.getSitesFunc(event.target.parentNode.getAttribute("label"), event.target);
  24.             }, false);
  25.             
  26.             var newMenuItemInChevron = document.createElement("menu");
  27.             newMenuItemInChevron.setAttribute("label", strTag);
  28.             newMenuItemInChevron.setAttribute("tooltipText", "")
  29.             newMenuItemInChevron.setAttribute("class", "relatedTopicMenuButton menu-iconic");
  30.             newMenuItemInChevron.setAttribute("image", "chrome://topsite/content/images/category" + i + ".png");
  31.                         if (this.chevron.firstChild)
  32.                             this.chevron.firstChild.appendChild(newMenuItemInChevron);
  33.                         else
  34.                             this.chevron.appendChild(newMenuItemInChevron);
  35.             var newMenuInMenuButton = document.createElement("menupopup");
  36.             newMenuItemInChevron.appendChild(newMenuInMenuButton);
  37.             newMenuItemInChevron.addEventListener("popupshowing", function f(event){
  38.                 myView.getSitesFunc(event.target.parentNode.getAttribute("label"), event.target);
  39.             }, false);
  40.             newMenuItemInChevron.collapsed = true;
  41.         }
  42.         if (arrTags.length > 0) {
  43.             this.topicsContainer.previousSibling.collapsed = false;
  44.         } else {
  45.             var newToolbarButton = document.createElement("toolbarbutton");
  46.             newToolbarButton.setAttribute("label", "No Results: Try Your Own");
  47.             newToolbarButton.setAttribute("class", "relatedTopicButton");
  48.             newToolbarButton.setAttribute("type", "button");
  49.             newToolbarButton.setAttribute("image", "chrome://topsite/content/images/category0.png");
  50.             newToolbarButton.setAttribute("oncommand", "topsite.overlay.openUrlInBrowser('http://www.topsite.com/?r=noresults');");
  51.             this.topicsContainer.appendChild(newToolbarButton);
  52.         }
  53.         var t = this;
  54.         setTimeout(function(){t.updateChevron();},200);
  55.     }
  56.  
  57.     
  58.     this.clear = function(){
  59.         while (this.topicsContainer.firstChild){
  60.             this.topicsContainer.removeChild(this.topicsContainer.firstChild);            
  61.                         if (this.chevron.firstChild && this.chevron.firstChild.firstChild)
  62.                             this.chevron.firstChild.removeChild(this.chevron.firstChild.firstChild);
  63.         }
  64.         this.topicsContainer.previousSibling.collapsed = true;
  65.         this.chevron.collapsed = true;
  66.     }
  67.  
  68.     this.showTopSite = function(arrTopSite, elmMenu){
  69.         if (elmMenu.childNodes.length > 0)
  70.             return;
  71.         var originalStrTopic = elmMenu.parentNode.getAttribute("label");
  72.         var strTopic = topsite.common.capitalizeWords(originalStrTopic);
  73.         var titleItem = document.getElementById("topSitePopup4TopicTitle").cloneNode(true);
  74.         titleItem.lastChild.setAttribute("value", topsite.consts.STRING_BUNDLE.GetStringFromName("popup.title").replace("%S", strTopic));
  75.         elmMenu.appendChild(titleItem);
  76.         elmMenu.appendChild(document.createElement("menuseparator"));
  77.         for (var i = 0; i < arrTopSite.length; ++i){
  78.             var strDisplayUrl = arrTopSite[i][0];
  79.             var strNavUrl = arrTopSite[i][1];
  80.             var isAdv = arrTopSite[i][2];
  81.             var newMenuItem = document.createElement("menuitem");
  82.             newMenuItem.setAttribute("label", strDisplayUrl);
  83.             newMenuItem.setAttribute("url", strNavUrl);
  84.             newMenuItem.setAttribute("class", "menuitem-iconic");
  85.             newMenuItem.setAttribute("isAdv", isAdv);
  86.             elmMenu.appendChild(newMenuItem);
  87.             var myView = this;
  88.             newMenuItem.onclick = function(event){
  89.                 myView.onSiteClickFunc(event.target.getAttribute("url"), originalStrTopic, event.target.getAttribute("isAdv"));
  90.             }
  91.             this.addFavIcon(strDisplayUrl, newMenuItem, "image", "chrome://topsite/content/images/noicon.png");
  92.         }
  93.  
  94.         if (arrTopSite.length==0) {
  95.             var newMenuItem = document.createElement("menuitem");
  96.             newMenuItem.setAttribute("label", "No top sites found, please try again later.");
  97.             newMenuItem.setAttribute("class", "menuitem-iconic");
  98.             elmMenu.appendChild(newMenuItem);
  99.         }
  100.  
  101.         elmMenu.appendChild(document.createElement("menuseparator"));
  102.         var footerItem = document.getElementById("topSitePopup4TopicFooter").cloneNode(true);
  103.         elmMenu.appendChild(footerItem);
  104.         footerItem.firstChild.onclick = function(event){
  105.             topsite.overlay.findTopSiteForTerm(strTopic);
  106.         }
  107.     }
  108.  
  109.     this.init = function(){
  110.         var t = this;    
  111.         window.addEventListener("resize",
  112.  function f(e) { t.updateChevron(e); }, false);
  113.  
  114.         this.chevron.addEventListener("popupshowing", function f2(e2) { t.chevronPopupShowing(e2); }, false);
  115.     
  116. }        
  117.     
  118.     this.chevronPopupShowing = function(aEvent){
  119.         var popup = aEvent.target;
  120.  
  121.               if (popup != this.chevron.firstChild)
  122.  
  123.                     return;
  124.  
  125.  
  126.         for (var i = 0; i < popup.childNodes.length; i++)
  127.  
  128.                     popup.childNodes[i].collapsed = !this.topicsContainer.childNodes[i].collapsed;
  129.  
  130.     }
  131.  
  132.     this.getElementWidth = function(element){
  133.         var style = document.defaultView.getComputedStyle(element, "");
  134.  
  135.             var leftMargin = style.getPropertyValue("margin-left");
  136.  
  137.         leftMargin = leftMargin ? Math.round(parseFloat(leftMargin)) : 0;
  138.  
  139.             var rightMargin = style.getPropertyValue("margin-right");
  140.  
  141.             rightMargin = rightMargin ? Math.round(parseFloat(rightMargin)) : 0;
  142.  
  143.               return element.boxObject.width + leftMargin + rightMargin;
  144.  
  145.     }        
  146.  
  147.     this.updateChevron = function(event){
  148.         if (event && event.target != document && event.target != window && event.target != content)
  149.  
  150.                     return;
  151.  
  152.  
  153.         if (this.topicsContainer.childNodes.length == 0) {
  154.  
  155.                     this.chevron.collapsed = true;
  156.  
  157.                     return;
  158.           
  159.         }
  160.  
  161.  
  162.         var spaceLeft = this.topicsContainer.boxObject.width;
  163.  
  164.               this.chevron.collapsed = false;
  165.  
  166.               var chevronWidth = this.chevron.boxObject.width;
  167.  
  168.               var overflowed = false;
  169.         for (var i = 0; i < this.topicsContainer.childNodes.length; i++) {            
  170.                     var child = this.topicsContainer.childNodes[i];
  171.  
  172.                     child.collapsed = false;
  173.  
  174.                     spaceLeft -= this.getElementWidth(child);
  175.  
  176.                     var spaceNeeded = (i == this.topicsContainer.childNodes.length - 1) ? 0 : chevronWidth;
  177.  
  178.                     if (spaceLeft < spaceNeeded) {
  179.  
  180.                           overflowed = true;
  181.  
  182.                           child.collapsed = true;
  183.  
  184.                     }
  185.  
  186.               }
  187.         this.chevron.collapsed = !overflowed;
  188.  
  189.     }
  190.  
  191.     this.addFavIcon = function(linkUrl, menuItem, attr, noImage){
  192.         var cuesrc = this.getUrlPrePath(linkUrl) + "/favicon.ico";            
  193.             menuItem.onerror = function () {
  194.                    this.setAttribute(attr, noImage);
  195.         }            
  196.             menuItem.setAttribute(attr, cuesrc);                               
  197.     } 
  198.  
  199.     this.getUrlPrePath = function(url){
  200.             try {
  201.             var fullUrl = url;
  202.             if (fullUrl.indexOf("://") < 0){
  203.                 if (fullUrl.indexOf(".") == fullUrl.lastIndexOf("."))
  204.                     fullUrl = "www." + fullUrl;
  205.                 fullUrl = "http://" + fullUrl;
  206.             }
  207.                     var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  208.                     var uri = ios.newURI(fullUrl, null, null);                
  209.                 return uri.prePath;
  210.             }
  211.             catch (e) {
  212.                 return url;
  213.             }
  214.     }
  215.  
  216.     this.init();        
  217. }
  218.  
  219. topsite.toolbarView.prototype = new topsite.view();
  220. topsite.toolbarView.prototype.constructor = topsite.view;